package com.ruoyi.system.service.impl;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.ruoyi.common.vo.GoodsQueryVo;
import com.ruoyi.system.domain.GoodsCategory;
import com.ruoyi.system.domain.GoodsChoice;
import com.ruoyi.system.domain.vo.GoodsSearchVO;
import com.ruoyi.system.mapper.GoodsChoiceMapper;
import com.ruoyi.system.service.GoodsCategoryService;
import com.ruoyi.system.service.GoodsChoiceService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * (GoodsChoice)表服务实现类
 *
 * @author makejava
 * @since 2020-10-26 18:19:20
 */
@Service("goodsChoiceService")
public class GoodsChoiceServiceImpl implements GoodsChoiceService {
    @Resource
    private GoodsChoiceMapper goodsChoiceDao;
    @Resource
    private GoodsCategoryService categoryService;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public GoodsChoice queryById(Integer id) {
        return this.goodsChoiceDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @return 对象列表
     */
    @Override
    public List<GoodsChoice> queryAllByLimit(GoodsQueryVo goodsQueryVo) {
        if (goodsQueryVo.getName() != null) {
            goodsQueryVo.setName("%" + goodsQueryVo.getName() + "%");
        }
        return this.goodsChoiceDao.queryAllByLimit(goodsQueryVo);
    }

    /**
     * 批量添加数据
     *
     * @param goodsChoices 数据
     */
    @Override
    public void batchInsert(List<GoodsChoice> goodsChoices) {
        goodsChoiceDao.batchInsert(goodsChoices);
    }

    /**
     * 新增数据
     *
     * @param goodsChoice 实例对象
     * @return 实例对象
     */
    @Override
    public GoodsChoice insert(GoodsChoice goodsChoice) {
        String categoryId = goodsChoice.getCategoryId();
        GoodsCategory goodsCategory = categoryService.queryById(Integer.parseInt(categoryId));
        if (goodsCategory == null) return null;
//        String goodsUrl = goodsChoice.getGoodsUrl();
//        if (goodsChoice.getImage() != null && !"".equalsIgnoreCase(goodsChoice.getImage())) {
//            Optional.ofNullable(goodsUrl)
//                    .ifPresent(url -> goodsChoice.setImage(getGoodsPic(goodsUrl)));
//        }
        double price = Double.parseDouble(goodsChoice.getPrice());
        int commission = Integer.parseInt(goodsChoice.getCommission());
        commission = 100 - commission;
        double result = Double.parseDouble(commission + "") / 100;
        goodsChoice
                .setCategoryName(goodsCategory.getName())
                .setStatus("0")
                .setRemainNum(goodsChoice.getReceiveNum())
                .setMinePrice((price * result) + "")
                .setSales("0")
                .setTop(0)
                .setSendMsgCount(0);
        this.goodsChoiceDao.insert(goodsChoice);
        return goodsChoice;
    }

    /**
     * 修改数据
     *
     * @param goodsChoice 实例对象
     * @return 实例对象
     */
    @Override
    public GoodsChoice update(GoodsChoice goodsChoice) {
        String categoryId = goodsChoice.getCategoryId();
        GoodsCategory goodsCategory = categoryService.queryById(Integer.valueOf(categoryId));
        goodsChoice.setCategoryName(goodsCategory.getName());
        this.goodsChoiceDao.update(goodsChoice);
        return this.queryById(goodsChoice.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.goodsChoiceDao.deleteById(id) > 0;
    }

    @Override
    public String getGoodsPic(String url) {
        System.out.println("商品的URL：" + url);
        String id = "";
        int index = url.indexOf("?");
        String substring = url.substring(index + 1);
        String[] split = substring.split("&");
        for (String s : split) {
            String[] split1 = s.split("=");
            for (String s1 : split1) {
                if ("id".equalsIgnoreCase(s1)) {
                    id = split1[1];
                    break;
                }
            }
        }
        try {
            String resultO = HttpUtil.get("https://ec.snssdk.com/product/fxgajaxstaticitem?b_type_new=0&device_id=0&is_outside=1&id=" + id + "&preview=0");
            System.out.println(resultO);
            Object data = JSON.parse(resultO);
            Map resultObject = (Map) data;
            Map result = ((Map) (resultObject.get("data")));
            return result.get("img").toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    /***
     * 根据id查询商品数据
     * @param id
     * @return
     */
    @Override
    public GoodsChoice getGoodsChoiceById(int id) {
        GoodsChoice goodsChoice = this.goodsChoiceDao.getGoodsChoiceById(id);
        return goodsChoice;
    }

    @Override
    public List<GoodsChoice> getGoodsAll(GoodsSearchVO goodsSearchVO) {
        if (goodsSearchVO.getName() != null) {
            goodsSearchVO.setName("%" + goodsSearchVO.getName() + "%");
        }
        if (goodsSearchVO.getShopName() != null) {
            goodsSearchVO.setShopName("%" + goodsSearchVO.getShopName() + "%");
        }
        List<GoodsChoice> list = goodsChoiceDao.getGoodsAll(goodsSearchVO);
        return list;
    }

    @Override
    public Integer editGoodsUrl(GoodsChoice goodsChoice) {
        Integer count = goodsChoiceDao.editGoodsUrl(goodsChoice);
        return count;
    }

    @Override
    public Integer addGoods(GoodsChoice goodsChoice) {
        String categoryId = goodsChoice.getCategoryId();
        GoodsCategory goodsCategory = categoryService.queryById(Integer.valueOf(categoryId));
        goodsChoice.setCategoryName(goodsCategory.getName());
        goodsChoice.setTop(0);
        goodsChoice.setSendMsgCount(0);
        return goodsChoiceDao.addGoods(goodsChoice);
    }

    @Override
    public Integer top(GoodsChoice goodsChoice) {
        Integer maxTop = this.maxTop();
//        Integer top = maxTop + 1;
        goodsChoice.setTop(maxTop + 1);
        return goodsChoiceDao.top(goodsChoice);

    }

    @Override
    public Integer delTop(GoodsChoice goodsChoice) {
        goodsChoice.setTop(0);
        return goodsChoiceDao.delTop(goodsChoice);
    }

    @Override
    public String getGoodsUrlById(Integer id) {
        String goodsUrl = goodsChoiceDao.getGoodsUrlById(id);
        return goodsUrl;
    }

    public Integer maxTop() {
        return goodsChoiceDao.maxTop();
    }
}
